package com.chaodev.base.controller;

import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.model.CompanyInfo;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.CompanyService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/24 17:22
 **/
@RestController
@RequestMapping("company")
@Api(value = "")
public class CompanyInfoController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    CompanyService companyService;

    /**
     * 获取公司信息表列表分页信息
     * @author ''
     */
    @ApiOperation(value = "获取公司信息表列表分页", notes = "获取公司信息表列表分页信息")
    @GetMapping(value = {"list"})
    public Result list(int start, int limit, CompanyInfo tbCompanyInfo, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
        PageInfo<CompanyInfo> pageInfo = companyService.queryPageList(start, limit, tbCompanyInfo);
            result.setCode(0);
            result.setMsg("获取成功");
            result.setData(pageInfo);
            logService.logAdd(null, "获取公司信息表列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取公司信息表列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取公司信息表信息
     * @author ''
     */
    @ApiOperation(value = "获取公司信息表对象", notes = "获取公司信息表信息")
    @GetMapping(value = {"/getById"})
    public Result findById(String id,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
        CompanyInfo tbCompanyInfo = companyService.findDataById(id);
            result.setCode(0);
            result.setData(tbCompanyInfo);
            logService.logAdd(null, "获取公司信息表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取公司信息表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存公司信息表信息
     * @author ''
     */
    @ApiOperation(value = "保存公司信息表对象", notes = "保存公司信息表信息")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody CompanyInfo companyInfo,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            companyInfo.setcUser(userInfo.getUserId());
            companyService.saveData(companyInfo);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "保存公司信息表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存公司信息表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除公司信息表信息
     * @author ''
     */
    @ApiOperation(value = "删除公司信息表对象", notes = "删除公司信息表信息")
    @GetMapping(value = {"/delete"})
    public Result delete(String id,HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            companyService.deleteDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "删除公司信息表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除公司信息表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
